<template>
  <div>
    <!-- InfoUpdate -->
    <van-nav-bar title="个人信息更改" left-arrow @click-left="onClickLeft" />
    <div class="head-img">
      <img :src="userImg" alt="" />
      <van-uploader class="change-img" :after-read="afterRead" />
    </div>
    <van-form @submit="onSubmit" class="forms">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
      />
      <van-field
        v-model="mobile"
        name="mobile"
        label="手机号"
        placeholder="手机号"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          color="linear-gradient(to right, #ff6034, #ee0a24)"
          native-type="submit"
          >保存</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { NavBar, Uploader, Form, Field, Button, Toast } from "vant";
import { updateInfo, getUserInfo } from "@/network";

export default {
  name: "InfoUpdate",
  data() {
    return {
      username: "",
      password: "",
      mobile: "",
      img: "",
      userImg: "",
    };
  },

  components: {
    [NavBar.name]: NavBar,
    [Uploader.name]: Uploader,
    [Form.name]: Form,
    [Field.name]: Field,
    [Button.name]: Button,
    [Toast.name]: Toast,
  },
  methods: {
    onClickLeft() {
      this.$router.back();
    },
    async onSubmit(values) {
      const userMsg = this.$store.state.pwd;
      const info = {
        filePath: this.img,
        nickname: this.username,
        mobile: this.mobile,
        password: userMsg,
      };
      console.log(info);
      const res = await updateInfo(info);
      console.log(res);
      if (res.data.errno === 0) {
        Toast.success("更新信息成功");
        this.$router.back();
      }
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      this.img = file.content;
    },
  },
  async created() {
    const {
      data: { data },
    } = await getUserInfo();
    console.log(data);
    this.userImg = data.avatar;
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.head-img {
  width: 100%;
  height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
  }
  .change-img {
    width: 80px;
    background: #fff;
    height: 80px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    opacity: 0.7;
    border-radius: 50%;
    overflow: hidden;

    // &:hover {
    //   opacity: .5;
    // }
  }
}
.forms {
  width: 90%;
  margin: 0 auto;
}
</style>

